﻿using Microsoft.Extensions.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace ESTAR.Infrastructure
{
    public class ConfigSettings
    {
        public static Dictionary<string, string> EStarConnectionString { get; set; } = new Dictionary<string, string>();

        public static string ENodeConnectionString { get; set; }
        public static int NameServerPort { get; set; }

        public static int CommandServerPort { get; set; } = 9000;
        public static int BrokerProducerPort { get; set; }
        public static int BrokerConsumerPort { get; set; }
        public static int BrokerAdminPort { get; set; }

        /// <summary>
        /// RPC端口
        /// </summary>
        public static int RPCPort { get; set; }

        /// <summary>
        /// REDIS地址
        /// </summary>
        public static string RedisConntion { get; set; }

        public static void Initialize(IConfiguration config)
        {
            if (config != null)
            {
                if (config.GetSection("ConnString").GetChildren() != null && config.GetSection("ConnString").GetChildren().Count() > 0)
                {
                    foreach (var item in config.GetSection("ConnString").GetChildren())
                    {
                        EStarConnectionString.Add(item.Key, item.Value);
                    }
                    if (EStarConnectionString["Enode"] == null)
                    {
                        throw new System.Exception("未配置ES链接字符串");
                    }
              
                    ENodeConnectionString = EStarConnectionString["Enode"];
                }
                if (config.GetSection("RPCPort").Value != null)
                {
                    RPCPort = int.Parse(config.GetSection("RPCPort").Value);
                }
                if (config.GetSection("RedisConntion").Value != null)
                {
                    RedisConntion =config.GetSection("RedisConntion").Value;
                }
            }

            NameServerPort = 10191;
            BrokerProducerPort = 10192;
            BrokerConsumerPort = 10193;
            BrokerAdminPort = 10194;
        }
    }
}
